<?php
declare (strict_types = 1);

namespace app\common\service;

use think\facade\Request;
use app\common\model\ZadanPlayerinfo as M;
use app\common\validate\ZadanPlayerinfo as V;

class ZadanPlayerinfo
{
    // 添加
    public static function goAdd($data)
    {
        //验证
        $validate = new V;
        if(!$validate->scene('add')->check($data))
        return ['msg'=>$validate->getError(),'code'=>201];
        try {
            M::create($data);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }
    
    // 编辑
    public static function goEdit($data,$id)
    {
        $data['id'] = $id;
        //验证
        $validate = new V;
        if(!$validate->scene('edit')->check($data))
        return ['msg'=>$validate->getError(),'code'=>201];
        try {
             M::update($data);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 状态
    public static function goStatus($data,$id)
    {
        $model =  M::find($id);
        if ($model->isEmpty())  return ['msg'=>'数据不存在','code'=>201];
        try{
            $model->save([
                'status' => $data,
            ]);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

     // 一键通过
     public static function onekeysend($ids)
     {
         if (!is_array($ids)) return ['msg' => '数据不存在', 'code' => 201];
         try {
             //查询配置，发送金币数量
             $configInfo = M::configInfo();
 
             foreach ($ids as $id) {
                 $model = M::find($id);
                 if ($model['isdefaultcoin'] == 2) {
                     continue;
                 }
                 M::sendCoin($model['actorid'], $model['account'], $model['qufu'], $configInfo["sendcoinlimit"], $model['cjcoin']);
             }
 
             return ['msg' => '操作成功', 'code' => 200];
         } catch (\Exception $e) {
             return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
         }
     }

    // 删除
    public static function goRemove($id)
    {
        $model = M::find($id);
        if ($model->isEmpty()) return ['msg'=>'数据不存在','code'=>201];
        try{
           $model->delete();
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 批量删除
    public static function goBatchRemove($ids)
    {
        if (!is_array($ids)) return ['msg'=>'数据不存在','code'=>201];
        try{
            M::destroy($ids);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 获取列表
    public static function goRecycle()
    {
        if (Request::isPost()){
            $ids = Request::param('ids');
            if (!is_array($ids)) return ['msg'=>'参数错误','code'=>'201'];
            try{
                if(Request::param('type')){
                    $data = M::onlyTrashed()->whereIn('id', $ids)->select();
                    foreach($data as $k){
                        $k->restore();
                    }
                }else{
                    M::destroy($ids,true);
                }
            }catch (\Exception $e){
                return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
            }
            return ['msg'=>'操作成功'];
        }
        //按用户名
        $where = [];
        $limit = input('get.limit');
        
               //按actorid查找
               if ($actorid = input("actorid")) {
                   $where[] = ["actorid", "like", "%" . $actorid . "%"];
               }
               //按账号查找
               if ($account = input("account")) {
                   $where[] = ["account", "like", "%" . $account . "%"];
               }
               //按区服查找
               if ($qufu = input("qufu")) {
                   $where[] = ["qufu", "like", "%" . $qufu . "%"];
               }
               //按开启盲盒次数查找
               if ($opennum = input("opennum")) {
                   $where[] = ["opennum", "like", "%" . $opennum . "%"];
               }
               //按超级金币数量查找
               if ($cjcoin = input("cjcoin")) {
                   $where[] = ["cjcoin", "like", "%" . $cjcoin . "%"];
               }
               //按是否赠送(1.未赠送 2.已赠送)查找
               if ($isdefaultcoin = input("isdefaultcoin")) {
                   $where[] = ["isdefaultcoin", "like", "%" . $isdefaultcoin . "%"];
               }
               //按玩家名称查找
               if ($nikename = input("nikename")) {
                   $where[] = ["nikename", "like", "%" . $nikename . "%"];
               }
        $list = M::onlyTrashed()->where($where)->paginate($limit);
        return ['code'=>0,'data'=>$list->items(),'extend'=>['count' => $list->total(), 'limit' => $limit]];
    }
}
